package com.akeil.flash.view;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.wb.swt.ResourceManager;
import org.eclipse.wb.swt.SWTResourceManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.akeil.flash.config.EDeviceType;
import com.akeil.flash.config.IConstantsConfig;
import com.akeil.flash.dataprocessing.IConstantProcessor;
import com.akeil.flash.event.DeviceInfoNotify;
import com.akeil.flash.pojo.Parameter;
import com.akeil.flash.service.IDataService;
import com.akeil.flash.service.IParameterService;

import flash.Application;

@Component
public class CalibrationView extends ViewPart implements Observer {
	public static final String ID = "com.akeil.flash.view.CalibrationView";
	private DecimalFormat df2 = new DecimalFormat("0.00");
	TableColumn tableColumn = null;
	private double atm;
	private double coupleTemperature;
	
	private Text textAtm;
	private Text textPressureCorrect;
	private Text textTemperatureCorrect;
	private Text textAtmCorrect;
	private Text textCoupleTemperature;
	List<Byte> cmds = new ArrayList<Byte>();
	List<Object> datas = new ArrayList<Object>();
	float atmCorr;
	float temperatureCorr;
	List<Parameter> parameters = null;
	@Autowired
	IParameterService parameterService;
	
	public CalibrationView() {
		
	}
	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) {
		parameters = parameterService.selectAll();
		parent.setLayout(new FillLayout(SWT.HORIZONTAL));
		atmCorr = parameters.get(0).getAtmcorr().floatValue();
		temperatureCorr = parameters.get(0).getTemperaturecorr().floatValue();

		Composite leftContainer = new Composite(parent, SWT.NONE);
		leftContainer.setLayout(new GridLayout(3, false));
		
		Label label = new Label(leftContainer, SWT.NONE);
		label.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		label.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label.setText("大气压强");
		
		textAtm = new Text(leftContainer, SWT.BORDER);
		textAtm.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		textAtm.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		textAtm.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label lblKpa = new Label(leftContainer, SWT.NONE);
		lblKpa.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		lblKpa.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		lblKpa.setText("kPa");
		
		Label label_1 = new Label(leftContainer, SWT.NONE);
		label_1.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		label_1.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_1.setText("压强修正");
		
		textPressureCorrect = new Text(leftContainer, SWT.BORDER);
		textPressureCorrect.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		textPressureCorrect.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		textPressureCorrect.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_2 = new Label(leftContainer, SWT.NONE);
		label_2.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		label_2.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		label_2.setText("℃");
		
		Label label_3 = new Label(leftContainer, SWT.NONE);
		label_3.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		label_3.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		label_3.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_3.setText("温度校准");
		
		textTemperatureCorrect = new Text(leftContainer, SWT.BORDER);
		textTemperatureCorrect.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		textTemperatureCorrect.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		textTemperatureCorrect.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		textTemperatureCorrect.setText(df2.format(temperatureCorr));
		
		Label label_7 = new Label(leftContainer, SWT.NONE);
		label_7.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		label_7.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		label_7.setText("℃");
		
		Label label_4 = new Label(leftContainer, SWT.NONE);
		label_4.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		label_4.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		label_4.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_4.setText("气压校准");
		
		textAtmCorrect = new Text(leftContainer, SWT.BORDER);
		textAtmCorrect.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		textAtmCorrect.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		textAtmCorrect.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		textAtmCorrect.setText(df2.format(atmCorr));
		
		Label label_6 = new Label(leftContainer, SWT.NONE);
		label_6.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		label_6.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		label_6.setText("kPa");
		
		Label label_5 = new Label(leftContainer, SWT.NONE);
		label_5.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		label_5.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		label_5.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		
		
		if(IConstantsConfig.deviceType == EDeviceType.MANUAL){
			label_5.setText("电偶温度");
		}else{
			label_5.setText("");
		}
		
		if(IConstantsConfig.deviceType == EDeviceType.MANUAL){
			textCoupleTemperature = new Text(leftContainer, SWT.BORDER);
			textCoupleTemperature.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
			textCoupleTemperature.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
			textCoupleTemperature.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
			textCoupleTemperature.setText("#.##");
		}		
			
		Label label_8 = new Label(leftContainer, SWT.NONE);
		label_8.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		label_8.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		
		if(IConstantsConfig.deviceType == EDeviceType.MANUAL){
			label_8.setText("℃");
		}else{
			label_8.setText("");
		}
		
		new Label(leftContainer, SWT.NONE);		
		Button btnSend = new Button(leftContainer, SWT.NONE);
		btnSend.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				parameters = parameterService.selectAll();
				parameters.get(0).setAtmcorr(Double.parseDouble(textAtmCorrect.getText().trim()));
				parameters.get(0).setTemperaturecorr(Double.parseDouble(textTemperatureCorrect.getText().trim()));
				parameterService.updateByPrimaryKey(parameters.get(0));
				
				cmds.clear();
				datas.clear();
				
				cmds.add(IConstantProcessor.ATMCorr);
				datas.add(Float.parseFloat(textAtmCorrect.getText()));
				
				cmds.add(IConstantProcessor.TempCorr);
				datas.add(Float.parseFloat(textTemperatureCorrect.getText()));
				
				if(cmds != null && cmds.size() > 0){
					Application.processor.sendParameters(cmds,datas);
				}
			}
		});
		GridData gd_btnSend = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_btnSend.heightHint = 42;
		btnSend.setLayoutData(gd_btnSend);
		btnSend.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		btnSend.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		btnSend.setText("发送参数");
		new Label(leftContainer, SWT.NONE);
		
		Composite rightContainer = new Composite(parent, SWT.NONE);
		rightContainer.setLayout(new FormLayout());
		
		Button buttonUp = new Button(rightContainer, SWT.NONE);
		buttonUp.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Application.processor.sendType(IConstantProcessor.ArmUp,0);
			}
		});
		buttonUp.setImage(ResourceManager.getPluginImage("Flash", "icons/calibration/up_32px.png"));
		buttonUp.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		buttonUp.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		FormData fd_buttonUp = new FormData();
		fd_buttonUp.bottom = new FormAttachment(0, 60);
		fd_buttonUp.left = new FormAttachment(100, -187);
		fd_buttonUp.top = new FormAttachment(0, 10);
		fd_buttonUp.right = new FormAttachment(100, -70);
		buttonUp.setLayoutData(fd_buttonUp);
		buttonUp.setText("悬臂上升");
		
		Button buttonDown = new Button(rightContainer, SWT.NONE);
		buttonDown.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Application.processor.sendType(IConstantProcessor.ArmDown,0);
			}
		});
		buttonDown.setImage(ResourceManager.getPluginImage("Flash", "icons/calibration/down_32px.png"));
		buttonDown.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		buttonDown.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		buttonDown.setText("悬臂下降");
		FormData fd_buttonDown = new FormData();
		fd_buttonDown.right = new FormAttachment(buttonUp, 0, SWT.RIGHT);
		fd_buttonDown.left = new FormAttachment(buttonUp, 0, SWT.LEFT);
		fd_buttonDown.bottom = new FormAttachment(buttonUp, 68, SWT.BOTTOM);
		fd_buttonDown.top = new FormAttachment(buttonUp, 18);
		buttonDown.setLayoutData(fd_buttonDown);
		
		Button buttonFire = new Button(rightContainer, SWT.NONE);
		buttonFire.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Application.processor.sendType(IConstantProcessor.Fire,0);
			}
		});
		buttonFire.setImage(ResourceManager.getPluginImage("Flash", "icons/calibration/fire_32px.png"));
		buttonFire.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		buttonFire.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		
		
		if(IConstantsConfig.deviceType == EDeviceType.MANUAL){
			buttonFire.setText("开盖点火");
		}else{
			buttonFire.setText("扫描点火");
		}
		
		FormData fd_buttonFire = new FormData();
		fd_buttonFire.bottom = new FormAttachment(buttonDown, 69, SWT.BOTTOM);
		fd_buttonFire.top = new FormAttachment(buttonDown, 19);
		fd_buttonFire.left = new FormAttachment(buttonUp, 0, SWT.LEFT);
		fd_buttonFire.right = new FormAttachment(100, -70);
		buttonFire.setLayoutData(fd_buttonFire);
		// 其他设置
		Application.flashData.addObserver(this);
	}
	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
	}
	
	@Override
	public void update(Observable o, Object notify) {
		// TODO 自动生成的方法存根
		if (notify instanceof DeviceInfoNotify) {
			processData(notify);
		}
	}
	
	private void processData(Object notifyEvent){
		DeviceInfoNotify deviceInfoNotify = (DeviceInfoNotify)notifyEvent;
		switch(deviceInfoNotify.getNotifyType()){
		case ATM_INFO:
			atm = deviceInfoNotify.getValue();
			textAtm.getDisplay().asyncExec(new Runnable(){

				@Override
				public void run() {
					// TODO 自动生成的方法存根
					textAtm.setText(df2.format(atm));
				}
				
			});
			
			textPressureCorrect.getDisplay().asyncExec(new Runnable(){

				@Override
				public void run() {
					// TODO 自动生成的方法存根
					textPressureCorrect.setText(df2.format(0.25 * (101.3 - atm)));
				}
			});
			
			break;
			
		case DESKTOP_INFO:
					
			break;
					
		case COUPLE_INFO:
			coupleTemperature = deviceInfoNotify.getValue();
			
			textCoupleTemperature.getDisplay().asyncExec(new Runnable(){

				@Override
				public void run() {
					// TODO 自动生成的方法存根
					textCoupleTemperature.setText(df2.format(coupleTemperature));
				}
				
			});
			
			break;
		}
	}
}